package com.huluobo.a2307monthdemo2.video

import androidx.lifecycle.MutableLiveData
import com.huluobo.lib_base.bean.CommentBean
import com.huluobo.lib_base.bean.VideoBean
import com.huluobo.lib_base.db.DouYinDatabase
import com.huluobo.lib_base.mvvm.BaseViewModel
import com.huluobo.lib_base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

/**
 *  Created by LC on 2025/10/8.
 */
@HiltViewModel
class VideoViewModel @Inject constructor(private val apiService: ApiService, private val db: DouYinDatabase) :
    BaseViewModel() {
    val getVideosSuccess by lazy { MutableLiveData<List<VideoBean>>() }
    fun getVideos(currentPage: Int, pageSize: Int) {
        executeNetwork(
            { apiService.getVideos(currentPage, pageSize) },
            getVideosSuccess::postValue
        )
    }

    val likeVideoSuccess by lazy { MutableLiveData<String>() }
    fun likeVideo(videoId: Int) {
        executeNetwork(
            { apiService.likeVideo(videoId) },
            likeVideoSuccess::postValue
        )
    }

    val unLikeVideoSuccess by lazy { MutableLiveData<String>() }
    fun unLikeVideo(videoId: Int) {
        executeNetwork(
            { apiService.unLikeVideo(videoId) },
            unLikeVideoSuccess::postValue
        )
    }

    val followAuthSuccess by lazy { MutableLiveData<String>() }
    fun followAuth(map: Map<String, Any>) {
        executeNetwork(
            { apiService.followAuth(mapToJson(map)) },
            followAuthSuccess::postValue
        )
    }

    val getCommentSuccess by lazy { MutableLiveData<List<CommentBean>>() }
    fun getComment(videoId: Int) {
        executeNetwork(
            { apiService.getComment(videoId) },
            getCommentSuccess::postValue
        )
    }

    val sendCommentSuccess by lazy { MutableLiveData<List<CommentBean>>() }
    fun sendComment(map: Map<String, Any>) {
        executeNetwork(
            { apiService.sendComment(mapToJson(map)) },
            sendCommentSuccess::postValue
        )
    }

    val getChildCommentSuccess by lazy { MutableLiveData<List<CommentBean>>() }
    fun getChildComment(currentPage: Int, pageSize: Int, rootId: Int, videoId: Int) {
        executeNetwork(
            { apiService.getChildComment(currentPage, pageSize, rootId, videoId) },
            getChildCommentSuccess::postValue
        )
    }

    val responseCommentSuccess by lazy { MutableLiveData<List<CommentBean>>() }
    fun responseComment(map: Map<String, Any>) {
        executeNetwork(
            { apiService.responseComment(mapToJson(map)) },
            responseCommentSuccess::postValue
        )
    }

    val collectVideoSuccess by lazy { MutableLiveData<String>() }
    fun collectVideo(videoBean: VideoBean) {
        executeDb(
            {
                db.getVideoDao().insertVideoInfo(videoBean)
                "收藏成功"
            },
            collectVideoSuccess::postValue
        )
    }
}